var rptType=5
var rpttime=1
var areaid=3501
var secret='2020abc'
var provincesText= Array()
var ids = Array()	
var hid=['china',0,0]
var pid=0
var nid='china'
var areacode=100000
var areaname='china'
function getData(url) {
	var randNum = Math.floor(Math.random() * 1000000000)
	var jiamistr ='areaid=' + areaid + 'randNum=' + randNum + 'rpttime=' + rpttime + 'rptType=' + rptType
	var jiami = $.md5(jiamistr) + secret;
	var sign = $.md5(jiami)
	var edata = Array()
	var areas = Array()
	var nums = Array()
	var all = Array()
	var sum = 0
	$.ajax({
		url: url,
		data: {
			rpttime,
			rptType,
			areaid,
			randNum,
			sign
		},
		dataType: 'json',
		type: 'get',
		success: function(data) {
			if(data.code==0){
				data.data.details.forEach(function(item, key) {
					areas.push(item.areaName)
					nums.push(item.iNum)
					all.push({
						'value': item.iNum,
						'name': item.areaName
					})
					sum += item.iNum
				})
				edata['areas'] = areas
				edata['nums'] = nums
				edata['all'] = all
			}
			echart1(edata)
			new Rendnum(sum, '.num')
			new Rendnum(sum, '.num1')
		}
	});
}

function getData1(url) {
	var randNum = Math.floor(Math.random() * 1000000000)
	var jiamistr = 'areaid=' + areaid + 'randNum=' + randNum + 'rpttime=' + rpttime + 'rptType=' + rptType
	var jiami = $.md5(jiamistr) + secret;
	var sign = $.md5(jiami)
	var edata = Array()
	var names = Array()
	var times = Array()
	var data1 = Array()
	var data2 = Array()
	var data3 = Array()
	var data4 = Array()
	var all = Array()
	var sum = 0
	$.ajax({
		url: url,
		data: {
			rpttime,
			rptType,
			areaid,
			randNum,
			sign
		},
		dataType: 'json',
		type: 'get',
		success: function(data) {
			if(data.code==0){
				$('.c-top ul').html('')
				data.data.classtotal.forEach(function(item, key) {
					names.push(item.className)
					sum += item.iNum
					all.push({
						'value': item.iNum,
						'name': item.className
					})
					$('.c-top ul').append(
						`
							<li>
								<div class="star star${key+1}">★</div>
								<div class="text">
									<p>${item.className}</p>
									<p>${item.iNum}<span>吨</span></p>
								</div>
							</li>
						`
					)
				})
				data.data.timedetails.forEach(function(item, key) {
					times.push(item.timeNote)
					data1.push(item.iTpyedata[0].iNum)
					data2.push(item.iTpyedata[1].iNum)
					data3.push(item.iTpyedata[2].iNum)
					data4.push(item.iTpyedata[3].iNum)
				})
				edata['names'] = names
				edata['times'] = times
				edata['data1'] = data1
				edata['data2'] = data2
				edata['data3'] = data3
				edata['data4'] = data4
				edata['all'] = all
			}else{
				edata=''
			}
			echart2(edata)
			echart4(edata)
		}
	});
}

function getData2(url) {
	var randNum = Math.floor(Math.random() * 1000000000)
	var jiamistr ='areaid=' + areaid + 'randNum=' + randNum + 'rpttime=' + rpttime + 'rptType=' + rptType
	var jiami = $.md5(jiamistr) + secret;
	var sign = $.md5(jiami)
	$.ajax({
		url: url,
		data: {
			rpttime,
			rptType,
			areaid,
			randNum,
			sign
		},
		dataType: 'json',
		type: 'get',
		success: function(data) {
			if(data.code==0){
				var col = ["#14a0a0", "#cd7517", "#284ad7", "#0f8b4f"]
				var tit = ["参与率", "知晓率", "准确率", "满意率"]
				Object.keys(data.data).forEach(function(item, key) {
					$(".circleChart#" + (key + 1)).circleChart({
						value: data.data[item] * 100,
						color: col[key],
						backgroundColor: "#081a4a",
						text: tit[key]
					});
					$('.m-left li .text').eq(key).html(data.data[item] * 100+'%')
				})
			}
		}
	});
}

function getData3(url) {
	var randNum = Math.floor(Math.random() * 1000000000)
	var jiamistr = 'areaid=' + areaid + 'randNum=' + randNum + 'rpttime=' + rpttime + 'rptType=' + rptType
	var jiami = $.md5(jiamistr) + secret;
	var sign = $.md5(jiami)
	var edata = Array()
	var areas = Array()
	var nums = Array()
	var all = Array()
	var sum = 0
	$.ajax({
		url: url,
		data: {
			rpttime,
			rptType,
			areaid,
			randNum,
			sign
		},
		dataType: 'json',
		type: 'get',
		success: function(data) {
			if(data.code==0){
				$('.m-right li .site').html(`${data.data.iSiteNum}<span>个</span>`)
				$('.m-right li .dudao').html(`${data.data.iWorkerNum}<span>人</span>`)
				$('.m-right li .car').html(`${data.data.iCarNum}<span>辆</span>`)
				data.data.sitedetails.forEach(function(item, key) {
					areas.push(item.areaName)
					nums.push(item.iSiteNum)
					all.push({
						'value': item.iSiteNum,
						'name': item.areaName
					})
				})
				edata['areas'] = areas
				edata['nums'] = nums
				edata['all'] = all
			}
			echart5(edata)
		}
	});
}



function getData5(url) {
	var randNum = Math.floor(Math.random() * 1000000000)
	var jiamistr = 'areaid=' + areaid + 'randNum=' + randNum + 'rpttime=' + rpttime + 'rptType=' + rptType
	var jiami = $.md5(jiamistr) + secret;
	var sign = $.md5(jiami)
	var edata = Array()
	var areas = Array()
	var nums = Array()
	var all = Array()
	var sum = 0
	$.ajax({
		url: url,
		data: {
			rpttime,
			rptType,
			areaid,
			randNum,
			sign
		},
		dataType: 'json',
		type: 'get',
		success: function(data) {
			if(data.code==0){
				data.data.forEach(function(item, key) {
					areas.push(item.state_name)
					nums.push(item.iNum)
					all.push({
						'value': item.iNum,
						'name': item.state_name
					})
					sum += item.iNum
				})
				edata['areas'] = areas
				edata['nums'] = nums
				edata['all'] = all
			}
			echart3(edata)
		}
	});
}

function getData4(url, cid, area,pName) {
	area = area || 'china'
	var randNum = Math.floor(Math.random() * 1000000000)
	var jiamistr = 'areaClass=' + cid + 'areaid=' + areaid + 'randNum=' + randNum + 'rpttime=' + rpttime + 'rptType=' + rptType
	var jiami = $.md5(jiamistr) + secret;
	var sign = $.md5(jiami)
	var edata = Array()
	var areas = Array()
	var nums = Array()
	var all = Array()
	var sum =0
	$.ajax({
		url: url,
		data: {
			rpttime,
			rptType,
			areaid,
			randNum,
			areaClass: cid,
			sign
		},
		dataType: 'json',
		type: 'get',
		success: function(data) {
			if(data.code==0){
				data.data.forEach(function(item, key) {
					areas.push(item.areaName)
					nums.push(item.itotalNum)
					ids.push(item.areaName)
					sum+=Number(item.itotalNum)
					ids[item.areaName]=item.areaid
					provincesText.push(item.areaName)
					provincesText[item.areaName]=item.itotalNum
					all.push({
						'value': item.itotalNum,
						'name': item.areaName
						
					})
				})
				new Rendnum(sum, '.num2')
				edata['nums'] = nums
				edata['all'] = all
				edata['info']=data.data
				// initEcharts(area, edata['all']);
			}else{
				alert('无此区域地图显示');
				pid--
				return;
			}
			loadMap(area, pName,edata['all']);
			getData('http://www.xuebanjicai.com:8166/Apps/ws/101/api/screen_data.asmx/getParticipatedata')
			getData1('http://www.xuebanjicai.com:8166/Apps/ws/101/api/screen_data.asmx/getClassData')
			getData2('http://www.xuebanjicai.com:8166/Apps/ws/101/api/screen_data.asmx/getSpreaddata')
			getData3('http://www.xuebanjicai.com:8166/Apps/ws/101/api/screen_data.asmx/getSiteData')
			getData5('http://www.xuebanjicai.com:8166/Apps/ws/101/api/screen_data.asmx/getFacilityData')
		}
	});
}		

function getData6(url, cid) {
	var randNum = Math.floor(Math.random() * 1000000000)
	var jiamistr = 'areaClass=' + cid + 'areaid=' + areaid + 'randNum=' + randNum + 'rpttime=' + rpttime + 'rptType=' + rptType
	var jiami = $.md5(jiamistr) + secret;
	var sign = $.md5(jiami)
	var sum=0
	$.ajax({
		url: url,
		data: {
			rpttime,
			rptType,
			areaid,
			randNum,
			areaClass: cid,
			sign
		},
		dataType: 'json',
		type: 'get',
		success: function(data) {
			if(data.code==0){
				var str=''
				data.data.forEach(function(item, key) {
					sum+=Number(item.itotalNum)
					ids[item.areaName]=item.areaid
					str+=`<li data="${item.areaid}"><span>${item.areaName}</span><span>${item.itotalNum}</span><span class="detail">详情</span></li>`
				})
				new Rendnum(sum, '.num2')
				$('#china-map').hide()
				$('.dqlist').show()
				$('.dqlist ul').html(`<div class="title"><div>`)
				$('.dqlist ul').html(str)
			}else{
				// alert('已经是最小单元了~~');
				pid--
				// return;
			}
			getData('http://www.xuebanjicai.com:8166/Apps/ws/101/api/screen_data.asmx/getParticipatedata')
			getData1('http://www.xuebanjicai.com:8166/Apps/ws/101/api/screen_data.asmx/getClassData')
			getData2('http://www.xuebanjicai.com:8166/Apps/ws/101/api/screen_data.asmx/getSpreaddata')
			getData3('http://www.xuebanjicai.com:8166/Apps/ws/101/api/screen_data.asmx/getSiteData')
			getData5('http://www.xuebanjicai.com:8166/Apps/ws/101/api/screen_data.asmx/getFacilityData')
		}
	});
}	

var myChart = echarts.init(document.getElementById('china-map'));
//存储切换的每一级地图信息
var mapStack = [];
var timeFn = null;
var curMap = {};
//初始化地图
areaid=hid[pid]
$('.index-dudao').get(0).href="weigui.html?areaid="+areaid
getData4('http://www.xuebanjicai.com:8166/Apps/ws/101/api/screen_data.asmx/getAreadata',pid,areacode,areaname)
/**
   绑定用户切换地图区域事件
   cityMap对象存储着地图区域名称和区域的信息(name-code)
   当mapCode有值,说明可以切换到下级地图
   同时保存上级地图的编号和名称  
*/
$('.dqlist ul').on('click','li',function(){
	pid++
	areaid=hid[pid]=$(this).attr('data')
	$('.index-dudao').get(0).href="weigui.html?areaid="+areaid
	$(this).siblings().removeClass('on')
	$(this).addClass('on')
	getData6('http://www.xuebanjicai.com:8166/Apps/ws/101/api/screen_data.asmx/getAreadata',pid)
	
})
function back(){
	pid--
	areaid=hid[pid]
	$('.index-dudao').get(0).href="weigui.html?areaid="+areaid
	if(pid<3){
		$('.dqlist').hide()
		$('#china-map').show()
		
		getData4('http://www.xuebanjicai.com:8166/Apps/ws/101/api/screen_data.asmx/getAreadata', pid, areacode,areaname)
	}else{
		getData6('http://www.xuebanjicai.com:8166/Apps/ws/101/api/screen_data.asmx/getAreadata',pid)
	}
}
myChart.on('mapselectchanged', function(params) {
		clearTimeout(timeFn);
	//由于单击事件和双击事件冲突，故单击的响应事件延迟250毫秒执行
	timeFn = setTimeout(function(){
			var name = params.batch[0].name;
			pid++
			nid=ids[name]
			hid[pid]=nid
			areaid=nid
			$('.index-dudao').get(0).href="weigui.html?areaid="+areaid
			var mapCode = cityMap[name];
			areaname=name
			if(mapCode){
				areacode=mapCode
			}
			console.log(areacode)
			if(pid>2){
				getData6('http://www.xuebanjicai.com:8166/Apps/ws/101/api/screen_data.asmx/getAreadata',pid)
				return;
			}
			getData4('http://www.xuebanjicai.com:8166/Apps/ws/101/api/screen_data.asmx/getAreadata', pid, areacode,areaname)
			//将上一级地图信息压入mapStack
			mapStack.push({
				mapCode: curMap.mapCode,
				mapName: curMap.mapName
			});	
		
	}, 250);
});
/**
   绑定双击事件，并加载上一级地图
*/
myChart.on('contextmenu', function(params) {
	event.preventDefault()
	flag=false
    //当双击事件发生时，清除单击事件，仅响应双击事件
    clearTimeout(timeFn);
	pid--
	if(pid<0){
		pid=0
	}
    var map = mapStack.pop();
	if (!mapStack.length && !map) {
	    alert('已经到达最上一级地图了');
		return;
	}
	areaid=hid[pid]
	$('.index-dudao').get(0).href="weigui.html?areaid="+areaid
	areacode=map.mapCode
	areaname=map.mapName
	
	getData4('http://www.xuebanjicai.com:8166/Apps/ws/101/api/screen_data.asmx/getAreadata', pid, areacode,areaname)

	getData('http://www.xuebanjicai.com:8166/Apps/ws/101/api/screen_data.asmx/getParticipatedata')
	getData1('http://www.xuebanjicai.com:8166/Apps/ws/101/api/screen_data.asmx/getClassData')
	getData2('http://www.xuebanjicai.com:8166/Apps/ws/101/api/screen_data.asmx/getSpreaddata')
	getData3('http://www.xuebanjicai.com:8166/Apps/ws/101/api/screen_data.asmx/getSiteData')
	getData5('http://www.xuebanjicai.com:8166/Apps/ws/101/api/screen_data.asmx/getFacilityData')

});
/**
   加载地图：根据地图所在省市的行政编号，
   获取对应的json地图数据，然后向echarts注册该区域的地图
   最后加载地图信息
   @params {String} mapCode:地图行政编号,for example['中国':'100000', '湖南': '430000']
   @params {String} mapName: 地图名称
*/
function loadMap(mapCode, mapName,data1) {
    $.getJSON('js/json/' + mapCode + '.json', function (data) {
	     if (data) {
		     echarts.registerMap(mapName, data);
			 var option = {
				tooltip: {
					trigger: 'item',
					formatter: '地区：{b} <br/> 重量：{c}'
				},
				 visualMap: {//颜色的设置  dataRange
					y: -1000,
					splitList: [
						{start: 0, end: 1, label: ''},
						{start: 1, end: 50000, label: '', color: '#00bbfd'}
					]
				},
				series: [
					{
						name: mapName,
						type: 'map',
						mapType: mapName,
						selectedMode : 'single',
						top: tp, //组件距离容器的距离
						zoom: 1.1,
						roam: false, //是否开启鼠标缩放和平移漫游
						label: {
							normal: {
								show: true
							},
							emphasis: {
								show: true
							}
						},
						data:data1,
						label: {
							normal: {
								// show: true,
								textStyle: {
									color: "#4b50f1"
								} //省份标签字体颜色
							},
							emphasis: { //对应的鼠标悬浮效果
								show: false,
								textStyle: {
									color: "#323232"
								}
							}
						},
						itemStyle: {
							normal: {
								borderWidth: .5, //区域边框宽度
								borderColor: '#10146f', //区域边框颜色
								areaColor: '#4d52f4'
						
							},
						
							emphasis: {
								borderWidth: .5,
								borderColor: '#fff',
								areaColor: '#4d52f4'
							}
						}
					}
				]
				
			 };
			 myChart.setOption(option);
			 curMap = {
				mapCode: mapCode,
				mapName: mapName
			 };		
		 } else {
             alert('无法加载该地图');
		 }		 
	});
}